<?php
namespace app\admin\controller;
use app\common\model\Major as MajorModel;
use app\common\service\CateService;
use think\facade\Request;
use app\admin\traits\Result;
use think\validate;
use think\Db;
use library\ArrayOperate;

class Major extends Base
{
    
    public function index(){
		$list = MajorModel::field('id,cate_pid as parent_id,cate_title as title')->where('status',1)->order('sort asc,id asc')->select();
		$data = ArrayOperate::arrayToTreexc($list);
		$tree_list = ArrayOperate::treeToArray($data);
        
        return $this->fetch('',['tree_list'=>$tree_list]);
    }
	
    public function edit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			
			$validate = validate('Major');
			if (!$validate->check($data)) {
				$this->error($validate->getError());
			}
			
			if(empty($data['cate_pid'])){
				$data['cate_level'] = 1;
			}else{
				$cate_level = Db::name('major')->where('id',$data['cate_pid'])->value('cate_level');
				$data['cate_level'] = $cate_level+1;
			}	
            if ($data['id']) {//编辑
                if($data['id'] == $data['cate_pid']){
                    $this->error('上级分类不能选自己');
                }
                $res = MajorModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $this->success('编辑成功');
                } else {
                    $this->error('编辑失败');
                }
            } else {//添加
                $MajorModel = new MajorModel;
                $res = $MajorModel->allowField(true)->save($data);
                if ($res) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            }
        } else { 
            $id = $this->request->param('id', 0, 'intval');
            if ($id) {
                $list = MajorModel::where('id', '=', $id)->find();
                $this->assign('list', $list);
            }
			$parent_id = $list['cate_pid']??0;
			
			$major_list = MajorModel::field('id,cate_pid as parent_id,cate_title as title')->where('status',1)->where('cate_level','in','1,2')->order('sort asc,id asc')->select();
        	$cate_option_str = ArrayOperate::arrayToSelect($major_list, $parent_id);
			//dump($cate_option_str);die;
			$this->assign('cate_option_str', $cate_option_str);
            return $this->fetch();
        }  
    }
    
    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $flag_cate = MajorModel::where('cate_pid',$id)->value('id');
            if(!empty($flag_cate)){
                $this->error('请先删除子分类');
            } 
            $res = MajorModel::where('id',$id)->delete();
            if ($res) {
                $this->success('删除成功');
            }else {
				$this->error('删除失败');
            }
        } else {
            $this->error('参数错误');
        }
        
    }
     
	public function getAllChildId(){
		$id = $this->request->param('id', 0, 'intval');
		if ($id) {
			$ids = getAllMajorChildId($id); 
			return $ids;
			 
		}else {
            $this->error('参数错误');
        }
        
    }
    
}



?>